#!/bin/bash

# nginx状态检查脚本
# 确认系统是否完全正常运行

echo "=== nginx状态检查 ==="

# 1. 检查nginx进程
echo "1. nginx进程状态:"
ps aux | grep nginx | grep -v grep

# 2. 检查监听端口
echo "2. 监听端口状态:"
netstat -tlnp | grep :8089

# 3. 检查nginx.pid文件
echo "3. nginx.pid文件状态:"
if [ -f /run/nginx.pid ]; then
    echo "✅ nginx.pid文件存在: /run/nginx.pid"
    echo "PID: $(cat /run/nginx.pid)"
else
    echo "⚠️ nginx.pid文件不存在"
fi

# 4. 检查错误日志
echo "4. nginx错误日志（最近5行）:"
sudo tail -5 /var/log/nginx/error.log

# 5. 检查访问日志
echo "5. nginx访问日志（最近5行）:"
sudo tail -5 /var/log/nginx/access.log

# 6. 测试API接口
echo "6. 测试API接口:"
API_RESPONSE=$(curl -s -w "HTTP状态码: %{http_code}\n" "http://localhost:8089/v3/api/InterfaceCommon/qExamMasterRecord?examSeq=251133887&dataType=1")
echo "$API_RESPONSE"

# 7. 测试DICOM文件访问
echo "7. 测试DICOM文件访问:"
DICOM_RESPONSE=$(curl -s -w "HTTP状态码: %{http_code}\n" "http://localhost:8089/files/20251030/DX/251133839_1.2.156.147522.44.410947.251133839.20251030074510/1.2.156.147522.44.410947.251133839.3.1.20251030074559.dcm")
echo "$DICOM_RESPONSE"

# 8. 检查系统资源
echo "8. 系统资源状态:"
echo "内存使用:"
free -h
echo ""
echo "磁盘使用:"
df -h /var/lib/nginx

echo "=== 状态检查完成 ==="
echo ""
echo "结果分析:"
echo "✅ 如果所有HTTP状态码都是200，表示系统正常"
echo "⚠️ 缓冲区警告是正常现象，不影响功能"
echo "✅ nginx进程和端口正常表示服务运行正常"
echo ""
echo "建议:"
echo "- 缓冲区警告可以忽略，这是nginx处理大数据的正常行为"
echo "- 系统现在应该完全正常运行"
echo "- 可以正常访问DICOM图像和API接口"